var getCityAll = require('../../utils/getCity.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  /**
   * 组件的初始数据
   */
  data: {
    isSelect: "",
    type: "",
    showSelect: "",
    jobSelectList: [],
    areaSelectList: [],
    sortSelectList: [],
    siftSelectList: [{
      "sex": "ALL"
    }],
    condition_name: [{
        "name": "类型",
        "type": "job",
        "title": "先择类型"
      },
      {
        "name": "区域",
        "type": "area",
        "title": "选择区域"
      },
      {
        "name": "排序",
        "type": "sort",
        "title": "选择排序"
      },
      {
        "name": "筛选",
        "type": "sift",
        "title": ""
      }
    ],
    condition_list: [{
        "title": "选择类型",
        "type": "job",
        "btnCloor": "selected",
        "items": [
          {
            "degree": "热门类型",
            "type": "job",
            "itemize": []
          },
          {
            "degree": "其他类型",
            "type": "job",
            "itemize": []
          }
        ]
      },
      {
        "title": "选择区域",
        "type": "area",
        "btnCloor": "selected",
        "items": [{
          "degree": "",
          "itemize": []
        }, ]
      },
      {
        "title": "选择排序",
        "type": "sort",
        "items": [{
          "degree": "",
          "itemize": [{
            "job": "最新发布",
            "isCheck": false,
            "type": "sort"
          }, {
            "job": "离我最近",
            "isCheck": false,
            "type": "sort"
          }]
        }]
      },
      {
        "title": "",
        "type": "sift",
        "items": [{//MALE FEMALE ALL
            "degree": "性别要求",
            "type": "sift",
            "itemize": [{
              "job": "不限",
              "type": "sift",
              "isCheck": true,
              "getType": "sex",
              "workTypeId": "ALL"
            }, {
              "job": "男",
              "type": "sift",
              "isCheck": false,
              "getType": "sex",
              "workTypeId": "MALE"
            }, {
              "job": "女",
              "type": "sift",
              "isCheck": false,
              "getType": "sex",
              "workTypeId": "FEMALE"
            }]
          }
        ]
      }
    ],
    cityArea: "",
    islogin: ""
  },
  pageLifetimes: {
    show: function attached() {
      const that = this;
      getApp().globalData.get("/api/work_type", {}).then(res=>{
        console.log(res.code)
        if (res.code == 200){
          let box = res.data;
          for (let i in box){
            if (box[i].hot){
              that.data.condition_list[0].items[0].itemize.push({
                job: box[i].content,
                type: "job",
                workTypeId: box[i].id,
                "isCheck": false,
              })
            }else {
              that.data.condition_list[0].items[1].itemize.push({
                job: box[i].content,
                type: "job",
                workTypeId: box[i].id,
                "isCheck": false,
              })
            }
          }
          that.setData({
            condition_list: that.data.condition_list,
            islogin: true
          })
        }else{
          that.setData({
            islogin: false
          })
        }
      })
      wx.getStorage({
        key: "selectCity",
        success: function(res) {
          var city = JSON.parse(res.data)
          getCityAll.getCityArea(city.citycode).then(res => {
            that.data.condition_list[1].items[0].itemize = res;
            that.setData({
              condition_list: that.data.condition_list
            })
          })
        },
        fail: function() {
          console.log("我失败了")
        }
      });
    }
  },
  /*
   * 组件的方法列表
   */
  methods: {
    selectType: function(e) { //选择类型、区域、排序、筛选等等选择项
      const that = this;
      const index = e.currentTarget.dataset.index;
      const type = e.currentTarget.dataset.type;
      if (that.data.isSelect != index || that.data.showSelect == "") {
        that.setData({
          isSelect: index,
          showSelect: "show",
          type: type
        })
      } else {
        that.setData({
          isSelect: "",
          showSelect: ""
        })
      }
    },
    checkboxChange: function(e) { //checkbox选项点击改变我要传输的值
      const that = this;
      switch (that.data.type) {
        case "job":
          that.setData({
            jobSelectList: e.detail.value,
          })
          break;
        case "area":
          that.setData({
            areaSelectList: e.detail.value,
          })
          break;
        case "sort":
          break;
        case "sift":
          break;
      }
    },
    selectAll: function(e) { //全选的状态下，所有子选择项目全部取消选择样式，和selected的值
      var that = this;
      var type = e.currentTarget.dataset.type;
      var box;
      if (type == "job") {
        box = that.data.condition_list[0].items;
        that.data.condition_list[0].btnCloor = "selected";
        that.setData({
          jobSelectList: [],
        })
      } else if (type == "area") {
        box = that.data.condition_list[1].items;
        that.data.condition_list[1].btnCloor = "selected";
        that.setData({
          areaSelectList: [],
        })
      }
      // 循环取消赋值false
      for (let i = 0; i < box.length; i++) {
        for (let j in box[i].itemize) {
          box[i].itemize[j].isCheck = false;
        }
      }
      that.setData({
        condition_list: that.data.condition_list,
      })
    },
    clickChange: function(e) { //单个选项可点击可取消的操作，值的改变和样式的改变
      var that = this;
      var type = e.currentTarget.dataset.type;
      var index = e.currentTarget.dataset.itemizeindex;
      var text = e.currentTarget.dataset.value;
      var rel = e.currentTarget.dataset.rel;
      var getType = e.currentTarget.dataset.gettype;
      var box;
      if (type == "job") {
        box = that.data.condition_list[0].items;
        that.data.condition_list[0].btnCloor = "";
      } else if (type == "area") {
        box = that.data.condition_list[1].items;
        that.data.condition_list[1].btnCloor = "";
      } else if (type == "sort") {
        box = that.data.condition_list[2].items;
        for (let i = 0; i < box[0].itemize.length; i++) {
          if (box[0].itemize[i].job == text) {
            box[0].itemize[i].isCheck = true;
            that.data.sortSelectList[0] = text;
            that.setData({
              sortSelectList: that.data.sortSelectList
            })
          } else {
            box[0].itemize[i].isCheck = false;
          }
        }
        that.setData({
          condition_list: that.data.condition_list
        })
        that.commitJobCondition();
        return;
      } else if (type == "sift") {
        box = that.data.condition_list[3].items;
        if (getType == "sex") {
          for (let i = 0; i < box[0].itemize.length; i++) {
            if (box[0].itemize[i].job == text) {
              box[0].itemize[i].isCheck = true;
              that.data.siftSelectList[0] = {
                "sex": rel
              };
              that.setData({
                siftSelectList: that.data.siftSelectList
              })
            } else {
              box[0].itemize[i].isCheck = false;
            }
          }
        } 
        // else if (getType == "time") {
        //   if (text == "不限") {
        //     box[1].itemize[0].isCheck = true;
        //     box[1].itemize[1].isCheck = false;
        //     box[1].itemize[2].isCheck = false;
        //     box[1].itemize[3].isCheck = false;
        //     that.data.siftSelectList[1] = {
        //       "time": ["不限"]
        //     };
        //   } else {
        //     if (that.data.siftSelectList[1].time.indexOf(text) != -1) {
        //       that.data.siftSelectList[1].time.splice(that.data.siftSelectList[1].time.indexOf(text), 1);
        //       box[1].itemize[index].isCheck = false;
        //       if (that.data.siftSelectList[1].time.length == 0){
        //         box[1].itemize[0].isCheck = true;
        //       }
        //     }else {
        //       box[1].itemize[0].isCheck = false;
        //       box[1].itemize[index].isCheck = true;
        //       that.data.siftSelectList[1].time.push(text);
        //     }
        //     if (that.data.siftSelectList[1].time.indexOf("不限") != -1) {
        //       that.data.siftSelectList[1].time.shift();
        //     }
        //   }
        // } else if (getType == "worktime") {
        //   if (text == "不限") {
        //     box[2].itemize[0].isCheck = true;
        //     box[2].itemize[1].isCheck = false;
        //     box[2].itemize[2].timeclass = "notime";
        //     that.data.siftSelectList[2] = {
        //       "worktime": "不限"
        //     };
        //   } else {
        //     // box[2].itemize[1].isCheck = true;
        //     // box[2].itemize[0].isCheck = false;
        //   }
        // } else if (getType == "period") {
        //   if (text == "不限") {
        //     box[3].itemize[0].isCheck = true;
        //     box[3].itemize[1].isCheck = false;
        //     box[3].itemize[2].isCheck = false;
        //     box[3].itemize[3].isCheck = false;
        //     that.data.siftSelectList[3] = {
        //       "period": ["不限"]
        //     };
        //   } else {
        //     if (that.data.siftSelectList[3].period.indexOf(text) != -1){
        //       that.data.siftSelectList[3].period.splice(that.data.siftSelectList[3].period.indexOf(text), 1);
        //       box[3].itemize[index].isCheck = false;
        //       if (that.data.siftSelectList[3].period.length == 0) {
        //         box[3].itemize[0].isCheck = true;
        //       }
        //     }else{
        //       box[3].itemize[0].isCheck = false;
        //       box[3].itemize[index].isCheck = true;
        //       that.data.siftSelectList[3].period.push(text);
        //     }
        //     if (that.data.siftSelectList[3].period.indexOf("不限") != -1) {
        //       that.data.siftSelectList[3].period.shift();
        //     }
        //   }
        // }
        that.setData({
          condition_list: that.data.condition_list,
          siftSelectList: that.data.siftSelectList
        })
        return;
      }
      // 点击可赋值，可取消
      for (let i = 0; i < box.length; i++) {
        for (let j in box[i].itemize) {
          if (box[i].itemize[j].job == text) {
            box[i].itemize[j].isCheck = !box[i].itemize[j].isCheck;
            that.setData({
              condition_list: that.data.condition_list
            })
          }
        }
      }
    },
    switchChange: function(e) {
      var that = this;
      var text = e.currentTarget.dataset.value;
      if (text == "只看高薪") {
        that.data.siftSelectList[4].isGaoXin = e.detail.value;
      } else {
        that.data.siftSelectList[5].isNearby = e.detail.value;
      }
      that.setData({
        siftSelectList: that.data.siftSelectList
      })
    },
    bindDateChange: function(e) {
      var that = this;
      that.data.condition_list[3].items[2].itemize[1].timevalue = e.detail.value;
      that.data.condition_list[3].items[2].itemize[2].job = e.detail.value;
      that.data.condition_list[3].items[2].itemize[2].timeclass = "hastime";
      that.data.condition_list[3].items[2].itemize[1].isCheck = true;
      that.data.condition_list[3].items[2].itemize[0].isCheck = false;
      that.data.siftSelectList[2] = {
        "worktime": e.detail.value
      };
      that.setData({
        siftSelectList: that.data.siftSelectList,
        condition_list: that.data.condition_list
      })
    },
    commitJobCondition: function() { //提交工作筛选条件
      var that = this;
      var job = that.data.jobSelectList.join(",");
      var area = that.data.areaSelectList.join(",");
      var sort = that.data.sortSelectList.join(",");
      var sift = that.getsift(that.data.siftSelectList);
      console.log(sort)
      if (sort == "最新发布"){
        var condition = {
          workTypeId: job,
          region: area,
          sex: sift.sex
        }
      }else{
        console.log(getApp().globalData)
        var condition = {
          workTypeId: job,
          region: area,
          sex: sift.sex,
          latitude: getApp().globalData.lat,
          longitude: getApp().globalData.lng,
        }
      }
      
      that.triggerEvent('getData', condition);
      that.setData({
        isSelect: "",
        showSelect: ""
      })
    },
    getsift: function(e) {
      var sex = e[0].sex;
      return {
        sex: sex,
      }
    },
    login: function(){
      wx.switchTab({
        url: '/pages/recruit/personal/personal',
      })
    }
  }
})